package cn.lll.vuehouse.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.lll.vuehouse.common.ResponseResult;
import cn.lll.vuehouse.common.ResponseStatus;
import cn.lll.vuehouse.dto.CustomerPageDTO;
import cn.lll.vuehouse.dto.ProviderPageDTO;
import cn.lll.vuehouse.entity.Customer;
import cn.lll.vuehouse.entity.Provider;
import cn.lll.vuehouse.service.ProviderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * @author: Cong
 * @date: 2023/9/5
 * @version: 1.0
 * @description:
 */
@RestController
@RequestMapping("provider")
public class ProviderController {

    @Autowired
    private ProviderService providerService;

    @GetMapping("page")
    public ResponseResult getPageList(ProviderPageDTO providerPageDTO) {
        return providerService.getProviderPage(providerPageDTO);
    }

    // getById 编辑客户时回显
    @GetMapping("getById")
    public ResponseResult getProviderInfo(Integer id) {
        return providerService.getProviderById(id);
    }

    // 修改客户状态 updateAvailaable
    @PutMapping("updateAvailable")
    public ResponseResult updateState(@RequestBody Provider provider) {
        providerService.updateById(provider);
        ResponseResult responseResult = new ResponseResult();
        responseResult.success();
        return responseResult;
    }

    // 添加和编辑请求
    @PostMapping("addOrUpdate")
    public ResponseResult addOrUpdateUser(@RequestBody Provider provider) {
        if (ObjectUtil.isNotNull(provider.getId())) {
            return providerService.updateUser(provider);
        }
        return providerService.addUser(provider);
    }

    // 批量删除用户数据
    @DeleteMapping("batchDelete")
    public ResponseResult batchDelete(@RequestBody Integer[] ids) {
        ResponseResult responseResult = new ResponseResult();
        List<Integer> list = Arrays.asList(ids);
        providerService.removeBatchByIds(list);
        responseResult.success();
        return responseResult;
    }

    @RequestMapping("list")
    public ResponseResult list() {
        List<Provider> list = providerService.list();
        ResponseResult responseResult = new ResponseResult();
        responseResult.success(ResponseStatus.SUCCESS,list);
        return responseResult;
    }


}

